<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\ProjectModel;
use common\service\ReportPushService;

class SendEveryDayReportConsumer extends BaseConsumer
{

    // 要消费的队列名
    public $queue = 'send_every_day_report';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 消费
     * @param $data
     * @return array|void
     */
    public function doConsume($data)
    {
        if (empty($data['project_id']) || empty($data['start_time']) || empty($data['end_time']) || empty($data['everyday_report_time']) || empty($data['role'])) {
            return [];
        }

        // 每日定时推送 如果推送时间已经更改,则忽略
        if (!empty($data['is_every_push']) && $data['is_every_push'] == 'yes') {
            $projectData = model(ProjectModel::class)->where(['id' => $data['project_id']])
                ->field([
                    'id',
                    "JSON_UNQUOTE(JSON_EXTRACT(json, '$.every_report_push_config.is_open_everyday_push')) as is_open_everyday_push",
                    "JSON_UNQUOTE(JSON_EXTRACT(json, '$.every_report_push_config.everyday_push_hour')) as everyday_push_hour",
                    "JSON_UNQUOTE(JSON_EXTRACT(json, '$.every_report_push_config.push_role')) as push_role"])
                ->find();
            if (empty($projectData)) {
                return;
            }
            // 每日定时推送按钮关闭
            if ($projectData['is_open_everyday_push'] != 'yes') {
                APP_DEBUG && trace(var_export($data, true), 'DEBUG');
                return;
            }
            // 如果本次队列的推送时间跟配置里的推送时间对不上,就说明配置已经更改,忽略本次推送
            if (empty($projectData['everyday_push_hour']) || $data['everyday_push_hour'] != $projectData['everyday_push_hour']) {
                APP_DEBUG && trace(var_export($data, true), 'DEBUG');
                return;
            }
        }
        try {
            APP_DEBUG && trace(var_export($data, true), 'DEBUG');
            ReportPushService::getInstance()->pushSpecifyRoleProjectMember($data['project_id'], $data['start_time'], $data['end_time'], $data['everyday_report_time'], $data['role'], $data['step_category_remark']);
        } catch (\Throwable $e) {
            trace(format_exception_info($e), "ERR");
        }
    }

}
